package com.example.myapplication.DesignPatterns.observerable;

import com.example.myapplication.thirdcamera.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者
 * 实现了Observerable接口，具体实现三个方法
 */
public class ObservedServer implements Observerable {

    private List<Observer> list;
    private String message;

    public ObservedServer() {
        list = new ArrayList<Observer>();
    }

    @Override
    public void registerObserver(Observer observer) {
        list.add(observer);
    }

    @Override
    public void deleteObserver(Observer observer) {
        list.remove(observer);
    }

    @Override
    public void notifyObserver() {
        for(int i = 0; i < list.size(); i++) {
            Observer oserver = list.get(i);
            oserver.updateMessage(message);
        }
    }

    public void setInfomation(String s) {
        this.message = s;
        Log.i("dove","微信服务更新消息： " + s);
        System.out.println("微信服务更新消息： " + s);
        //消息更新，通知所有观察者
        notifyObserver();
    }

}
